/*
 * 中北大学软件学院版权所有2020-2025
 */
package nuc.lp.uolab.competition.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import nuc.lp.uolab.competition.vo.Competition;
import nuc.lp.uolab.util.DbUtil;

/**
 * @author liupeng
 * @version 1.0
 */
public class CompetitionDaoImpl implements CompetitionDao{

	@Override
	public boolean insertCompetition(Competition competition) {
		boolean flag = false;
		Connection connection = null;
		PreparedStatement pStatement = null;

		connection = DbUtil.getConnection();
		String sql = "INSERT INTO t_competition VALUES(?,?,?,?,?,?,?,?,?)";
		try {
			pStatement = connection.prepareStatement(sql);
			pStatement.setInt(1, competition.getCid());
			pStatement.setString(2, competition.getCategories());
			pStatement.setString(3, competition.getYear());
			pStatement.setString(4, competition.getTopic());
			pStatement.setString(5, competition.getGroupleader());
			pStatement.setString(6, competition.getGroupmember());
			pStatement.setString(7, competition.getAward());
			pStatement.setString(8, competition.getAdviser());
			pStatement.setString(9, competition.getRemark());

			int cont = pStatement.executeUpdate();
			if (cont != 0) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.closeResource(connection, pStatement, null);
		}
		return flag;
	}

	@Override
	public boolean updateCompetition(Competition competition) {
		Connection connection = DbUtil.getConnection();
		PreparedStatement pStatement = null;
		boolean flag = false;

		String sql = "UPDATE t_competition SET groupmember = ?, award = ?";
		try {
			pStatement = connection.prepareStatement(sql);

			pStatement.setString(1, competition.getGroupmember());
			pStatement.setString(2, competition.getAward());
			// sql影响到的行数
			if (1 == pStatement.executeUpdate()) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.closeResource(connection, pStatement, null);
		}

		return flag;
	}

	@Override
	public List<Competition> selectCompetition(String year) {
		Connection connection = DbUtil.getConnection();
		PreparedStatement pStatement = null;
		ResultSet rSet = null;
		List<Competition> competitionrs = new ArrayList<Competition>();

		String sql = "SELECT * FROM t_competition WHERE year = ?";

		try {
			pStatement = connection.prepareStatement(sql);
			pStatement.setString(1, year);
			rSet = pStatement.executeQuery();
			// 把rSet结果集合中数据集合到userList中
			while (rSet.next()) {
				Competition competition = new Competition();
				// rs.get里面username是表中字段
				competition.setCid(rSet.getInt("c_id"));
				competition.setCategories(rSet.getString("categories"));
				competition.setYear(rSet.getString("year"));
				competition.setTopic(rSet.getString("topic"));
				competition.setGroupleader(rSet.getString("groupleader"));
				competition.setGroupmember(rSet.getString("groupmember"));
				competition.setAward(rSet.getString("award"));
				competition.setAdviser(rSet.getString("adviser"));
				competition.setRemark(rSet.getString("remark"));
				competitionrs.add(competition);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.closeResource(connection, null, rSet);
		}
		return competitionrs;
	}

}
